#!/bin/sh
################################################################################
# x
################################################################################
. /etc/rc.d/functions

start() {

    /bin/touch /var/lib/init/X.inprogress

    if [ "x${MM_MYTHTV_RUN_ON_EGLFS}" = "xyes" ] ; then

        /bin/chmod 777 -R /dev/input*
        /bin/chmod 777 -R /dev/vchiq

        /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/X] Starting myth in EGLFS mode..."
        mm_message_output info "Starting mythfrontend ..."

        /bin/rm -f /var/lib/init/X.inprogress

        mm_x_start

        /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/X] Started myth in EGLFS mode with return code=0..."

        return 0

    else

        local DRI_TRUE
        local LIB_PATH
        local LIB_PATHS
        local LIB
        local LIBS
        local NVIDIA_TRUE
        local INTEL_I810_TRUE
        local INTEL_I915_TRUE
        local RADEON_TRUE
        local VMWARE_TRUE
        local ARMSOC_TRUE
        local MESON_TRUE
        local VC4_TRUE
        local ROCKCHIP_TRUE
        local ALLWINNER_TRUE

        /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/X] Starting..."

        # Prepare directory where X-windows session information is saved
        /bin/rm -fr           /tmp/.ICE-unix/
        /bin/mkdir -m 1777 -p /tmp/.ICE-unix/
        /bin/chown root:root  /tmp/.ICE-unix/

        # Set xscreensaver type acordingly to config
        if [ "x${MM_X_SCREENSAVER_HACK}" = "xsleep" ]         ; then
            /bin/sed -i 's%@MODE@%blank%' /home/minimyth/.xscreensaver
            /bin/sed -i 's%@SELECTED@%0%' /home/minimyth/.xscreensaver
        elif [ "x${MM_X_SCREENSAVER_HACK}" = "xblank" ]       ; then
            /bin/sed -i 's%@MODE@%blank%' /home/minimyth/.xscreensaver
            /bin/sed -i 's%@SELECTED@%0%' /home/minimyth/.xscreensaver
        elif [ "x${MM_X_SCREENSAVER_HACK}" = "xglslideshow" ] ; then
            /bin/sed -i 's%@MODE@%one%'   /home/minimyth/.xscreensaver
            /bin/sed -i 's%@SELECTED@%1%' /home/minimyth/.xscreensaver
        else
            /bin/sed -i 's%@MODE@%blank%' /home/minimyth/.xscreensaver
            /bin/sed -i 's%@SELECTED@%0%' /home/minimyth/.xscreensaver
        fi
        /bin/sed -i "s%@MM_X_SCREENSAVER_TIMEOUT@%${MM_X_SCREENSAVER_TIMEOUT}%" /home/minimyth/.xscreensaver
        /bin/sed -i "s%@MM_MEDIA_SCREENSAVER_GALLERY_MOUNTPOINT@%${MM_MEDIA_SCREENSAVER_GALLERY_MOUNTPOINT}%" /home/minimyth/.xscreensaver

        # Hacks to deal with the fact that the names for proprietary drivers and
        # open source drivers conflict.
        #   the proprietary NVIDIA GL libraries conflict with the Open Source Mesa GL libraries.
        /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/X] Configuring Xorg libs for ${MM_X_DRIVER}..."
        NVIDIA_TRUE='#'
        NVIDIA_LEGACY_TRUE='#'
        INTEL_I810_TRUE='#'
        INTEL_I915_TRUE='#'
        RADEON_TRUE='#'
        VMWARE_TRUE='#'
        ARMSOC_TRUE='#'
        MESON_TRUE='#'
        VC4_TRUE='#'
        ROCKCHIP_TRUE='#'
        ALLWINNER_TRUE='#'
        case "${MM_X_DRIVER}" in
            intel_i810)
                /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/X] Enabling intel_i810 driver in Xorg.conf..."
                INTEL_I810_TRUE=''
                # Remove all Nvidia drivers
                /bin/rm -rf /lib/modules/*/misc/nvidia* /usr/lib/nvidia* /usr/lib/vdpau-nvidia* /usr/lib/vdpau/libvdpau_nvidia.so.1
                ;;
            intel_i915)
                /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/X] Enabling intel_i915 driver in Xorg.conf..."
                INTEL_I915_TRUE=''
                # Remove all Nvidia drivers
                /bin/rm -rf /lib/modules/*/misc/nvidia* /usr/lib/nvidia* /usr/lib/vdpau-nvidia* /usr/lib/vdpau/libvdpau_nvidia.so.1
                ;;
            nvidia)
                /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/X] Enabling nvidia driver in Xorg.conf..."
                NVIDIA_TRUE=''
                # Include path to proprietary libraries.
                LIB_PATHS=`/bin/cat /etc/ld.so.conf`
                LIB_PATHS="/usr/lib/nvidia ${LIB_PATHS}"
                /bin/echo -n > /etc/ld.so.conf
                for LIB_PATH in ${LIB_PATHS} ; do
                    /bin/echo "${LIB_PATH}" >> /etc/ld.so.conf
                done
                # Remove Open Source libraries that conflict with proprietary libraries.
                LIBS=`/usr/bin/find /usr/lib/nvidia -type f | /bin/sed -e "s%^.*/%%"`
                for LIB in ${LIBS} ; do
                    LIB_PATH=`/usr/bin/find /usr/lib -name ${LIB} | /bin/sed -e "s%^/usr/lib/nvidia/.*\$%%"`
                    if [ -n "${LIB_PATH}" ] ; then
                        /bin/rm -f ${LIB_PATH}
                    fi
                done
                # Remove legacy drivers
                /bin/rm -rf /usr/lib/nvidia-legacy/* /usr/lib/vdpau-nvidia-legacy/libvdpau_nvidia.so.1
                # Rebuild library cache.
                /sbin/ldconfig
                ;;
            nvidia_legacy)
                /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/X] Enabling nvidia legacy driver in Xorg.conf..."
                NVIDIA_LEGACY_TRUE=''
                # Include path to proprietary libraries.
                LIB_PATHS=`/bin/cat /etc/ld.so.conf`
                LIB_PATHS="/usr/lib/nvidia-legacy ${LIB_PATHS}"
                /bin/echo -n > /etc/ld.so.conf
                for LIB_PATH in ${LIB_PATHS} ; do
                    /bin/echo "${LIB_PATH}" >> /etc/ld.so.conf
                done
                # Remove Open Source libraries that conflict with proprietary libraries.
                LIBS=`/usr/bin/find /usr/lib/nvidia-legacy -type f | /bin/sed -e "s%^.*/%%"`
                for LIB in ${LIBS} ; do
                    LIB_PATH=`/usr/bin/find /usr/lib -name ${LIB} | /bin/sed -e "s%^/usr/lib/nvidia-legacy/.*\$%%"`
                    if [ -n "${LIB_PATH}" ] ; then
                        /bin/rm -f ${LIB_PATH}
                    fi
                done
                # Remove non-legacy drivers
                /bin/rm -rf /usr/lib/nvidia/* /usr/lib/vdpau/libvdpau_nvidia.so.1 /usr/bin/nvidia-smi
                # Move legacy libvdpau to /usr/lib/vdpau
                /bin/mv /usr/lib/vdpau-nvidia-legacy/libvdpau_nvidia.so.1 /usr/lib/vdpau/
                # Move legacy nvidia-smi to /usr/bin
                /bin/mv /usr/bin/nvidia-legacy/nvidia-smi /usr/bin/
                # Rebuild library cache.
                /sbin/ldconfig
                ;;
            radeon)
                /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/X] Enabling radeon driver in Xorg.conf..."
                RADEON_TRUE=''
                # Remove all Nvidia drivers
                /bin/rm -rf /lib/modules/*/misc/nvidia* /usr/lib/nvidia* /usr/lib/vdpau-nvidia* /usr/lib/vdpau/libvdpau_nvidia.so.1
                ;;
            vmware)
                /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/X] Enabling vmware driver in Xorg.conf..."
                VMWARE_TRUE=''
                # Remove all Nvidia drivers
                /bin/rm -rf /lib/modules/*/misc/nvidia* /usr/lib/nvidia* /usr/lib/vdpau-nvidia* /usr/lib/vdpau/libvdpau_nvidia.so.1
                ;;
            armsoc)
                /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/X] Enabling armsoc driver in Xorg.conf..."
                ARMSOC_TRUE=''
                # Remove all Nvidia drivers
                /bin/rm -rf /lib/modules/*/misc/nvidia* /usr/lib/nvidia* /usr/lib/vdpau-nvidia* /usr/lib/vdpau/libvdpau_nvidia.so.1
                /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/X] Unloading lima.ko ttm.ko gpu_sched kernel modules..."
                /sbin/rmmod lima ttm gpu_sched
                /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/X] Loading mali.ko kernel module..."
                /sbin/modprobe mali
                ;;
            meson)
                /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/X] Enabling meson driver in Xorg.conf..."
                MESON_TRUE=''
                # Remove all Nvidia drivers
                /bin/rm -rf /lib/modules/*/misc/nvidia* /usr/lib/nvidia* /usr/lib/vdpau-nvidia* /usr/lib/vdpau/libvdpau_nvidia.so.1
                ;;
            vc4)
                /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/X] Enabling vc4 driver in Xorg.conf..."
                VC4_TRUE=''
                # Remove all Nvidia drivers
                /bin/rm -rf /lib/modules/*/misc/nvidia* /usr/lib/nvidia* /usr/lib/vdpau-nvidia* /usr/lib/vdpau/libvdpau_nvidia.so.1
                ;;
            rockchip)
                /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/X] Enabling rockchip driver in Xorg.conf..."
                ROCKCHIP_TRUE=''
                # Remove all Nvidia drivers
                /bin/rm -rf /lib/modules/*/misc/nvidia* /usr/lib/nvidia* /usr/lib/vdpau-nvidia* /usr/lib/vdpau/libvdpau_nvidia.so.1
                /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/X] Setting v4l2_request as VA driver..."
                echo "export LIBVA_DRIVER_NAME=v4l2_request" >> /etc/conf.d/core
                ;;
            sun4i)
                /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/X] Enabling allwinner driver in Xorg.conf..."
                ALLWINNER_TRUE=''
                # Remove all Nvidia drivers
                /bin/rm -rf /lib/modules/*/misc/nvidia* /usr/lib/nvidia* /usr/lib/vdpau-nvidia* /usr/lib/vdpau/libvdpau_nvidia.so.1
                /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/X] Setting v4l2_request as VA driver..."
                echo "export LIBVA_DRIVER_NAME=v4l2_request" >> /etc/conf.d/core
                ;;
        esac

        /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/X] Preparing Xorg.conf file..."

        /bin/sed -i "s%@MM_X_DRIVER@%${MM_X_DRIVER}%"               /etc/X11/xorg.conf
        /bin/sed -i "s%@NVIDIA_TRUE@%${NVIDIA_TRUE}%"               /etc/X11/xorg.conf
        /bin/sed -i "s%@NVIDIA_LEGACY_TRUE@%${NVIDIA_LEGACY_TRUE}%" /etc/X11/xorg.conf
        /bin/sed -i "s%@INTEL_I810_TRUE@%${INTEL_I810_TRUE}%"       /etc/X11/xorg.conf
        /bin/sed -i "s%@INTEL_I915_TRUE@%${INTEL_I915_TRUE}%"       /etc/X11/xorg.conf
        /bin/sed -i "s%@RADEON_TRUE@%${RADEON_TRUE}%"               /etc/X11/xorg.conf
        /bin/sed -i "s%@VMWARE_TRUE@%${VMWARE_TRUE}%"               /etc/X11/xorg.conf
        /bin/sed -i "s%@ARMSOC_TRUE@%${ARMSOC_TRUE}%"               /etc/X11/xorg.conf
        /bin/sed -i "s%@MESON_TRUE@%${MESON_TRUE}%"                 /etc/X11/xorg.conf
        /bin/sed -i "s%@VC4_TRUE@%${VC4_TRUE}%"                     /etc/X11/xorg.conf
        /bin/sed -i "s%@ROCKCHIP_TRUE@%${ROCKCHIP_TRUE}%"           /etc/X11/xorg.conf
        /bin/sed -i "s%@ALLWINNER_TRUE@%${ALLWINNER_TRUE}%"         /etc/X11/xorg.conf

        # Make sure that the file ownership is correct.
        /bin/chown -Rh minimyth:minimyth /home/minimyth 2>/dev/null &

        mm_message_output info "Starting Xserver ..."

        /bin/rm -f /var/lib/init/X.inprogress

        mm_x_start

        /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/X] Started with return code=0..."

        return 0
    fi
}

stop() {
    if [ "x${MM_MYTHTV_RUN_ON_EGLFS}" = "xyes" ] ; then

        /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/X] Skipped (EGLFS mode) with return code=0..."
        return 0

    else

        mm_message_output info "Stopping Xserver ..."
        mm_x_stop
        return 0

    fi
}

case $1 in
    start) start ;;
    stop)  stop  ;;
esac

exit 0
